/**
 * SEVAS - Swiss Electronic Vaccination Advisor Service / Agile Development
 *
 * $Id: Application.java 29 2010-08-18 16:51:14Z zimmr7@bfh.ch $
 */
package ch.bfh.sevas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Age {

    public static final long MILLISECONDS_PER_DAY = 86400000;
    public static final Map<String, Integer> vaccinationAges;
    private static final Map<String, String> longVaccinationAges;

    private String vaccinationAge;
    private long ageInMilliSeconds;

    static {
        Map<String, Integer> aMap = new LinkedHashMap<String, Integer>();
        aMap.put("2m", 61);
        aMap.put("4m", 122);
        aMap.put("6m", 183);
        aMap.put("12m", 366);
        aMap.put("15m", 458);
        aMap.put("4j", 1464);
        aMap.put("11j", 4026);
        vaccinationAges = Collections.unmodifiableMap(aMap);

        Map<String, String> bMap = new LinkedHashMap<String, String>();
        bMap.put("2m", "2 months");
        bMap.put("4m", "4 months");
        bMap.put("6m", "6 months");
        bMap.put("12m", "12 months");
        bMap.put("15m", "15 months");
        bMap.put("4j", "4 years");
        bMap.put("11j", "11 years");
        longVaccinationAges = Collections.unmodifiableMap(bMap);
    }

    public static List<String> getElapsedVaccinationAges(Age age) {
        List<String> result = new ArrayList<String>();

        for (Map.Entry<String, Integer> entry : vaccinationAges.entrySet()) {
            if ((entry.getValue() * 9 / 10) <= age.AgeInDays()) {
                result.add(entry.getKey());
            }
        }

        return result;
    }

    public static String getLongVaccinationAge(String vaccinationAge) {
        return longVaccinationAges.get(vaccinationAge);
    }

    public Age(Date birthdate, Date date) {
        this.vaccinationAge = "";
        this.ageInMilliSeconds = date.getTime() - birthdate.getTime();
    }

    public Age(int days) {
        this.vaccinationAge = "";
        this.ageInMilliSeconds = days * MILLISECONDS_PER_DAY;
    }

    public Age(String vaccinationAge, int days) {
        this.vaccinationAge = vaccinationAge;
        this.ageInMilliSeconds = days * MILLISECONDS_PER_DAY;
    }

    public int AgeInDays() {
        return (int) (ageInMilliSeconds / MILLISECONDS_PER_DAY);
    }

    public String getVaccinationAge() {
        return this.vaccinationAge;
    }
}
